package rullekake.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import rullekake.entities.Faculty;
import rullekake.entities.Institution;
import rullekake.entities.StudyProgram;

/**
 * Klasse som holder styr på hva brukeren har valgt av fakultet, institusjon og studieprogram.
 * 
 */
@ManagedBean (name="mainListSelection")
@SessionScoped
public class MainListSelection {
    Faculty faculty;
    Institution institution;
    StudyProgram studyProgram;

    @ManagedProperty (value="#{institutionController}")
    private InstitutionController institutionController;

    @ManagedProperty (value="#{facultyController}")
    private FacultyController facultyController;

    @ManagedProperty (value="#{studyProgramController}")
    private StudyProgramController spController;

    @ManagedProperty (value="#{courseController}")
    private CourseController courseController;

    public CourseController getCourseController() {
        return courseController;
    }

    public void setCourseController(CourseController courseController) {
        this.courseController = courseController;
    }

    public StudyProgramController getSpController() {
        return spController;
    }

    public void setSpController(StudyProgramController spController) {
        this.spController = spController;
    }


    public FacultyController getFacultyController() {
        return facultyController;
    }

    public void setFacultyController(FacultyController facultyController) {
        this.facultyController = facultyController;
    }



    public InstitutionController getInstitutionController() {
        return institutionController;
    }

    public void setInstitutionController(InstitutionController institutionController) {
        this.institutionController = institutionController;
    }


    public MainListSelection() {
    }

    public Faculty getFaculty() {
        return faculty;
    }

    public void setFaculty(Faculty faculty) {
        this.faculty = faculty;
        this.institution = faculty.getInstitution();
    }

    public boolean isFacultySelected() {
        return faculty != null;
    }

    public Institution getInstitution() {
        return institution;
    }

    public String selectInstitution() {
        institutionController.prepareEdit();
        setInstitution(institutionController.getSelected());
        facultyController.setFacultiesByInstitution(institution);
        return null;
    }

    public String selectFaculty() {
        facultyController.prepareEdit();
        setFaculty(facultyController.getSelected());
        spController.setStudyProgramsByFaculty(faculty);
        return null;
    }
    
    public String selectStudyProgram() {
        spController.prepareEdit();
        setStudyProgram(spController.getSelected());
        courseController.setCoursesByStudyProgram(studyProgram);
        return null;
    }


    public void setInstitution(Institution institution) {
        this.institution = institution;
    }

    public boolean isInstitutionSelected() {
        return institution != null;
    }

    public StudyProgram getStudyProgram() {
        return studyProgram;
    }

    public void setStudyProgram(StudyProgram studyProgram) {
        this.studyProgram = studyProgram;
        setFaculty(studyProgram.getFaculty());
    }

    public boolean isStudyProgramSelected() {
        return studyProgram != null;
    }

    /**
     * Hjelpemetoder som sørger for at ting blir tegnet i riktig rekkefølge.
     * @return
     */

    public boolean renderCourseTable() {
        return (isStudyProgramSelected());
    }

    public boolean renderFacultyTable() {
        return (isInstitutionSelected() && !isFacultySelected());
    }

    public boolean renderInstitutionTable() {
        return !isInstitutionSelected();
    }

    public boolean renderStudyProgramTable() {
        return (isFacultySelected() && !isStudyProgramSelected());
    }

    public String reset() {
        faculty = null;
        institution = null;
        studyProgram = null;
        return null;
    }
}
